var websocket;
var audio = document.getElementById('event_video'); 

//视频控制
var video = {
	timer:null,
	audioStat : false,
	reset : function(){
		 audio.currentTime = 0;
	},
	play : function(){
		if(audio!==null){
			video.audioStat = true;
			 for(var i=1;i<5;i++){
	    		var data={};
				data["from"]="1";
				data["fromName"]="yrx";
				data["to"]=i;
				data["text"]="bf"+Math.ceil(audio.currentTime);
	    		websocket.send(JSON.stringify(data));
			}
		}
	},
	monitoring : function (){
		//关闭之前开启的定时事件
		if(video.timer != null){
			window.clearInterval(video.timer);
		}
		var time1 = audio.currentTime;
		video.timer = setInterval(function(){
			time2 = time1;
			time1 = audio.currentTime;
			console.info(time1-time2);
			if((time1-time2)>1){
				console.info("快进了");
				for(var i=1;i<5;i++){
	        		var data={};
					data["from"]="1";
					data["fromName"]="yrx";
					data["to"]=i;
					data["text"]="dwbf"+Math.ceil(time1);
	        		websocket.send(JSON.stringify(data));
				}
	        }else if(audio.paused && video.audioStat){
	        	video.audioStat = false;
	    		for(var i=1;i<5;i++){
	        		var data={};
					data["from"]="1";
					data["fromName"]="yrx";
					data["to"]=i;
					data["text"]="zt"+Math.ceil(time1);
	        		websocket.send(JSON.stringify(data));
				}
	        }
			if(time1 == audio.duration || time1 == 0){
				window.clearInterval(timer);
				console.info("暂停了");
			}
		},300);
	}
}

var user = {
	id : "",
	eventsid : ""
}

$(document).ready(function() {
		//用户的全局唯一标识：事件id+个人id，中间以逗号隔开
		var user_guid = "1";
		//初始化websocket
      	if ('WebSocket' in window) {
			websocket = new WebSocket("ws://127.0.0.1:8080/yl-websocket/ws?uid="+user_guid);
		} else if ('MozWebSocket' in window) {
			websocket = new MozWebSocket("ws://127.0.0.1:8080/yl-websocket/ws?uid="+user_guid);
		} else {
			websocket = new SockJS("http://127.0.0.1:8080/yl-websocket/ws/sockjs?uid="+user_guid);
		}
      	websocket.onmessage = function(event) {
     	var data=JSON.parse(event.data);
     	if(data.from>0||data.from==-1){
         
        }else if(data.from==0){
        	
        }else if(data.from==-2){
        	  
        }
    };
  });
